const app = getApp()
var that;
var util = require('../../../utils/util.js');
Page({

  /* 页面的初始数据 */
  data: {
    userinfo: ''
  },

  /* 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    that = this
    that.setData({
      userinfo: app.globalData.loginUserObj
    })
  },

  // 更改组织机构名称
  toEditInfoFun: function () {
    wx.navigateTo({
      url: '/pages/company/companyEdit/companyEdit',
    })
  },

  // 转让管理员身份
  toChangeBossFun:function(){
    wx.navigateTo({
      url: '/pages/company/companyBossChange/companyBossChange',
    })
  },

  // 成员管理
  toSetMemberFun: function () {
    wx.navigateTo({
      url: '/pages/company/companyMember/companyMember',
    })
  },

  // 关闭模式
  switchFun:function(e){
    let closing = 0;
    if(e.detail.value){
      closing = 1;
    }else{
      closing = 0;
    }
    wx.request({
      url: app.http + 'hysCompany/closeCompany',
      data: {
        companyId: app.globalData.loginUserObj.ydCompany.id,
        userId: app.globalData.loginUserObj.id,
        closing: closing
      },
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        if (res.data.code == 200) {
          
          if (closing == 1){
            wx.showToast({
              title: '已开启',
            })
          }else{
            wx.showToast({
              title: '已关闭',
            })
          }
          var pages = getCurrentPages();
          var prevPage = pages[0]; //上一个页面
          if (prevPage.route == "pages/pageMe/pageMe") {
            prevPage.getuserinfoJD();
          }
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.switchFun(e);
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.switchFun(e);
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("fail返回", err)
      }
    })
  },

  //解散组织
  dissolutionJD() {
    
    wx.request({
      url: app.http + 'hysCompany/deleteCompany',
      data: {
        companyId: app.globalData.loginUserObj.ydCompany.id,
        userId: app.globalData.loginUserObj.id,
      },
      method: 'DELETE',
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '已解散组织',
            icon: 'success',
            duration: 2000,
            success() {
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }, 1000)
            }
          })
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.dissolutionJD();
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.dissolutionJD();
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("fail返回", err)
      }
    })
  }

})